package spring.boot.server.handler.heartbeat;

import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import spring.boot.server.codec.Invocation;
import spring.boot.server.dispatcher.MessageHandler;
import spring.boot.server.message.heartbeat.HeartbeatRequest;
import spring.boot.server.message.heartbeat.HeartbeatResponse;

@Slf4j
@Component
public class HeartbeatRequestHandler implements MessageHandler<HeartbeatRequest> {

    @Override
    public void execute(Channel channel, HeartbeatRequest message) {
        log.debug("Netty服务端收到客户端[{}]的心跳请求,num=[{}]", channel.id(), message.getNum());
        // 响应心跳
        HeartbeatResponse response = new HeartbeatResponse(message.getNum());
        channel.writeAndFlush(new Invocation(HeartbeatResponse.TYPE, response));
    }

    @Override
    public String getType() {
        return HeartbeatRequest.TYPE;
    }

}
